package auntie.production.app2;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import auntie.production.app2.db.auntie_production_app_db.production_details;
import auntie.production.app2.db.auntie_production_app_db.productions;
import auntie.production.app2.svc.s1;

public class Page2 extends s1 {

    Button btn_add_product;
    Button btn_backtomain;
    TextView tv_total;
    TextView tv_totallbl;
    TextView tv_msg;
    ListView lv_products;
    LvAdp_Products lvadp_products;
    AlertDialog alert_dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page2);

        btn_add_product = (Button) findViewById(R.id.btn_add_product);
        btn_backtomain = (Button) findViewById(R.id.btn_backtomain);
        tv_total = (TextView) findViewById(R.id.tv_total);
        tv_totallbl = (TextView) findViewById(R.id.tv_totallbl);
        tv_msg = (TextView) findViewById(R.id.tv_msg);
        lv_products = (ListView) findViewById(R.id.lv_products);
        lvadp_products = new LvAdp_Products(this);
        lv_products.setAdapter(lvadp_products);

        btn_add_product.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                on_add_product();
            }
        });

        btn_backtomain.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                backtomain();
            }

        });

        
        lv_products.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> av, View vw, int pos,
                    long arg3) {
                on_lv_products(av, vw, pos);
            }
        });

        hlp_clean();
    }

    @Override
    protected void onResume() {
        super.onResume();
        hlp_update_lvadp_products();
    }
    
    @Override
    public void onBackPressed() {
        // do nothing
    }
    
    private void backtomain() {
       Intent browse = new Intent();
       browse.setClass(Page2.this, Main.class);
       startActivity(browse);
    }

    private void hlp_alert_dialog_show(final production_details pd) {
        final CharSequence[] items = {"Cancel", "UnCancel"};
        AlertDialog.Builder alert_dialog_builder = new AlertDialog.Builder(this);
        alert_dialog_builder.setTitle("Cancelation");
        alert_dialog_builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if( item == 0) {
                    pd.setCancelled(true);
                    svc_update_production_details(pd);
                    hlp_update_lvadp_products();
                    return;
                } 
                if( item == 1) {
                    pd.setCancelled(false);
                    svc_update_production_details(pd);
                    hlp_update_lvadp_products();
                    return;
                } 
                
            }
        });

        alert_dialog = alert_dialog_builder.create();
        alert_dialog.show();
    }

   
    private void hlp_update_lvadp_products() {
        Bundle b = getIntent().getExtras();
        if (b == null)
            return;

        productions p = (productions) b.getSerializable(s1.s1_productions);
        List<production_details> pds = svc_all_production_details_by_production(p.getId());
        if( pds == null || pds.isEmpty() ) {
            hlp_clean();
            return;
        }
        
        double total_amount = 0.0;
        int size = pds.size();
        for( production_details pd : pds ) {
            if( pd.isCancelled() ) continue;
            total_amount += pd.getAmount();
        }
        
        p.setTotal_amount(total_amount);
        svc_update_productions(p);
        
        tv_totallbl.setText("Total");
        tv_total.setText(MessageFormat.format("{0,number,#,##0.00}",  total_amount));
        tv_msg.setText(MessageFormat.format("there are {0} record(s) all", size));
        
        lvadp_products.items.clear();
        lvadp_products.items.addAll(pds);
        lv_products.setAdapter(lvadp_products);
        
    }
    
   
    private void hlp_clean() {
        tv_total.setText("");
        tv_totallbl.setText("");
        tv_msg.setText("");
    }

    // action
    void on_add_product() {
        Bundle b = getIntent().getExtras();
        productions p = (productions) b.getSerializable(s1.s1_productions);
        
        Intent browse = new Intent();
        browse.setClass(Page2.this, Page3.class);
        browse.putExtra(s1.s1_productions, p);
        startActivity(browse);
    }

    void on_lv_products(AdapterView<?> av, View vw, int pos) {
        production_details pd = (production_details) av.getItemAtPosition(pos);
        hlp_alert_dialog_show(pd);
    }

    // class adapter
    static class LvAdp_Products extends BaseAdapter {

        public final List<production_details> items;
        private Context ctx;

        public LvAdp_Products(Context ctx) {
            this.ctx = ctx;
            items = new ArrayList<production_details>();
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return items.indexOf(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = (RelativeLayout) View.inflate(ctx,
                    R.layout.item_product, null);

            TextView item_pno = (TextView) convertView
                    .findViewById(R.id.item_pno);
            TextView item_product = (TextView) convertView
                    .findViewById(R.id.item_product);
            TextView item_time = (TextView) convertView
                    .findViewById(R.id.item_time);
            TextView item_sticker = (TextView) convertView
                    .findViewById(R.id.item_sticker);
            TextView item_ptotal = (TextView) convertView
                    .findViewById(R.id.item_ptotal);

            production_details pd = items.get(position);
            item_pno.setText((position + 1) + "");

            String p_time = MessageFormat.format("{0,date,h:mm a}", pd.getInput_time());
            String p_total = MessageFormat.format("{0,number,#,##0.00}",
                    pd.getAmount());
            String p_price = MessageFormat.format("{0,number,#,##0.00}", pd.getPrice());
            String p_yield = MessageFormat.format("{0,number,#,###}", pd.getQuantity());
            
            String f_product = pd.getProduct_name()+"@"+p_price;
            String f_sticker = pd.getSticker() + " = " + p_yield + "@" + p_price;
            if (pd.isCancelled()) {
                item_product.setText(f_product);
                item_product.setPaintFlags(item_product.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);
                item_time.setText(p_time);
                item_time.setPaintFlags(item_time.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);
                
                item_sticker.setText(f_sticker);
                item_sticker.setPaintFlags(item_sticker.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);
                
                item_ptotal.setText(p_total);
                item_ptotal.setPaintFlags(item_ptotal.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);
            } else {
                item_product.setText(f_product);
                item_time.setText(p_time);
                item_sticker.setText(f_sticker);
                item_ptotal.setText(p_total);
            }

            return convertView;
        }

        
    } // LvAdp_Products

}
